<?php
namespace app\Sysadmin\Controller;
use think\Db;
class Adminrole extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function tips(){
		return $this->fetch('common/success');
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('status','name');
		$_get_str = "";
		
		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);
		
		return $this->fetch();
	}
	
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			
			$_data = array();
			$_data['name'] = $_post_data['name'];
			$_data['info'] = $_post_data['info'];
			$_data['status'] = $_post_data['status'];
			$_data['create_time'] = time();
			$_data['update_time'] = time();
			if(!$_data['name']){
				$this->error('角色名称不能为空！');exit;
			}
			if(Db::name('admin_role')->where('name','like',$_data['name'])->count()){
				$this->error('角色名称重复！');exit;
			}
			$_ret_id = Db::name('admin_role')->insertGetId($_data);
			if($_ret_id){
				$this->add_operation_log($_ret_id,$_data);
				cache('sysadmin_role_lists',null);
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}
		$_info = array(
			'id'=>'0',
			'name'=>'',
			'status'=>'1',
			'info'=>'',
		);
		$this->assign('info',$_info);
		$this->assign('page_name','添加角色');
		return $this->fetch('info');
	}
	
	public function edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			$_id = (int)$_post_data['id'];
			if(!$_id){
				$this->error('系统错误！');exit;
			}
			$_data = array();
			$_data['name'] = $_post_data['name'];
			$_data['info'] = $_post_data['info'];
			$_data['status'] = $_post_data['status'];
			$_data['update_time'] = time();
			if(!$_data['name']){
				$this->error('角色名称不能为空！');exit;
			}
			if(Db::name('admin_role')->where('name','like',$_data['name'])->where('id','<>',$_id)->count()){
				$this->error('角色名称重复！');exit;
			}
			$_ret_id = Db::name('admin_role')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				cache('sysadmin_role_lists',null);
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('admin_role')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		
		$this->assign('page_name','编辑角色');
		$this->assign('info',$_info);
		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");exit;
			}
			$_info = Db::name('admin_role')->where('id','=',$_id)->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");exit;
			}
			if($this->get_num($_info['id']) > 0){
				$this->error("该角色存在管理员，请移除管理员以后再进行删除");exit;
			}
			$_ret = Db::name('admin_role')->where('id','=',$_id)->delete();
			if($_ret){
				$this->add_operation_log($_id,$_info);
				cache('sysadmin_role_lists',null);
				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}
			
		}else{
			$this->error("系统错误！");
		}
	}
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(isset($_input_data['status'])){
			if(in_array($_input_data['status'],array('0','1'))){
				$_where[] = array('status','=',$_input_data['status']);
			}
		}
		if(!empty($_input_data['name'])){
			$_where[] = array('name','like','%'.$_input_data['name'].'%');
		}
		$count = Db::name('admin_role')->where($_where)->count();
		$lists = Db::name('admin_role')->where($_where)->page($page.','.$limit)
					->field('id,name,info,status')->order('id asc')->select();
		
		if($lists){
			foreach($lists as $k=>$v){
				$lists[$k]['num'] = $this->get_num($v['id']);
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	
	private function get_num($role_id=0){
		$_s_name = 'admin_num_'.$role_id;
		$_count = cache($_s_name);
		if($_count){
			return $_count;
		}
		$_where = array();
		if($role_id != 0){
			$_where[] = array('role_id','=',$role_id);
		}
		$_count = Db::name('admin')->where($_where)->count();
		cache($_s_name,$_count,60);
		return $_count;
	}
}
?>